package com.dengc;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class GameWin extends JFrame {

    //金块集合
    List<Object>  objectList = new ArrayList<>();
    //创造一个绘制背景类
    Bg bg =new Bg();
    //创造一个绘制线类
    Line line = new Line(this);
    //整个游戏状态 0未开始，1运行中，2商店，3失败，4胜利
    static int state;
    //定义一张画布
    Image offScreenImage;

    {
        //是否可以创建
        boolean isPlace = true;
        //创建金块
        for (int i = 0; i < 10; i++) {
            double random = Math.random();
            //当前生成的金块
            Gold gold;
            if (random < 0.3){gold = new GoldMini();}
            else if (random < 0.7){gold = new Gold();}
            else {gold = new GoldPlus();}
            for (Object obj:objectList){
                if (gold.getRec().intersects(obj.getRec())) {
                    //重叠发送，不能生成重新生成
                    isPlace = false;
                }
            }
            if (isPlace) {objectList.add(gold);}
            else {isPlace = true; i--;}
        }
        //创建石块
        for (int i = 0; i < 5; i++) {
            Rock rock;
            //缺少石头图片，只生成一种石头
            rock = new Rock();
            for (Object obj:objectList){
                if (rock.getRec().intersects(obj.getRec())) {
                    //重叠发送，不能生成重新生成
                    isPlace = false;
                }
            }
            if (isPlace) {objectList.add(rock);}
            else {isPlace = true; i--;}
        }
    }

    void launch() throws InterruptedException {
        //窗口是否可见
        this.setVisible(true);

        //设置窗口大小
        this.setSize(768,1000);

        //设置监听鼠标功能
        this.setLocationRelativeTo(null);

        //设置标题
        this.setTitle("黄金矿工之挖掘机");

        //设置关闭窗口事件
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //抓取点击事件
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                switch (state){
                    case 0:
                        //点击开始游戏
                        if (e.getButton()==3){
                            state=1;
                            //赋值系统时间
                            bg.startTime = System.currentTimeMillis();
                        }
                        break;
                    case 1:
                        //左右摇摆
                        if (e.getButton()==1&&line.status==0){
                            line.status = 1;
                        }
                        //抓取返回,点击右键
                        if (e.getButton()==3 && line.status==3 && Bg.waterNum>0){
                            Bg.waterFlag = true;
                            Bg.waterNum--;
                        }
                        break;
                    case 2:
                        if (e.getButton()==1){
                            bg.shop = true;
                        }
                        if (e.getButton()==3){
                            state = 1;
                            bg.startTime = System.currentTimeMillis();
                        }
                        break;
                    case 3:
                    case 4:
                        if (e.getButton()==1){
                            state = 0;
                            bg.reGame();
                            line.reGame();
                        }
                        break;
                    default:
                }

            }
        });

        //不断绘制实现线不断摆动
        while (true){
            repaint();
            nextLevel();
            Thread.sleep(10);
        }
    }

    @Override
    public void  paint(Graphics g){
       offScreenImage = this.createImage(768,1000);
       Graphics gImage = offScreenImage.getGraphics();

       bg.paintSelf(gImage);

       //游戏开始再绘制线与物体
       if (state == 1){
            //遍历所有金块
           for (Object obj: objectList) {
               obj.paintSelf(gImage);
           }
           try {
               line.paintSelf(gImage);
           } catch (InterruptedException e) {
               throw new RuntimeException(e);
           }
       }
       g.drawImage(offScreenImage,0,0,null);
    }

    public static void main(String[] args) throws InterruptedException {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }

    //下一关方法
    public void  nextLevel() throws InterruptedException {
        if(state == 1 && bg.gameTime()){
            if (Bg.count >= bg.goal){
                //过多少关后成功
                if (Bg.level == 10) {
                    state = 4;
                } else {
                    state = 2;
                    //关卡数+1
                    Bg.level++;
                }
            }else {
                state = 3;
            }
            dispose();
            GameWin gameWin = new GameWin();
            gameWin.launch();
        }
    }
}
